package com.example.xiaosuan.zyfypt_304_;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView yyzh;
    private EditText username, password;
    private Button btnregister, btnreset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        yyzh = (TextView) findViewById(R.id.yiyouzhanghao);
        username = (EditText) findViewById(R.id.ResUserName);
        password = (EditText) findViewById(R.id.RegPassword);
        btnregister = (Button) findViewById(R.id.btn_register);
        btnreset = (Button) findViewById(R.id.btn_reset);
        btnreset.setOnClickListener(this);
        yyzh.setOnClickListener(this);
        btnregister.setOnClickListener(this);
        username.setFocusable(true);
        //自动调出输入法
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        //用户名Edittext获取焦点
        username.setFocusable(true);
        username.setFocusableInTouchMode(true);
        username.requestFocus();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yiyouzhanghao:
//                Toast.makeText(this,"点击已有账号",Toast.LENGTH_SHORT).show();
//                finish();//结束当前界面，返回上一界面
                Intent intent2 = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent2);
                break;
            case R.id.btn_register:
                doRegister();
                break;
            case R.id.btn_reset:
                username.setText("");
                password.setText("");
                Toast.makeText(this, "点击重置按钮", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void doRegister() {
        if(("".equals(username.getText().toString())||username.getText()==null)||(
                "".equals(password.getText().toString())||password.getText()==null
                )){
            Toast.makeText(getApplicationContext(), "用户名密码不能为空或位数不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        String[] reg = new String[1];
        StringBuffer sb = new StringBuffer();
        String temp = sb.append(username.getText().toString().trim())
                .append(":")
                .append(password.getText().toString().trim()).toString();
        reg[0] = temp;
        LoginActivity loginactivity = new LoginActivity();
        loginactivity.setDUMMY_CREDENTIALS(reg);
        for (String str : loginactivity.getDUMMY_CREDENTIALS()){
            System.out.println(str);
        }
        username.setText("");
        password.setText("");
        createAlertDialog();

    }

    //AlertDialog对话框
    public void createAlertDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("注册成功，是否去登录");
        builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent2);
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
            }
        });
        builder.create().show();
    }
}
